package com.base.controller.uum;

import com.base.api.uum.UumUserService;
import com.base.utils.RandomValidateCodeUtil;
import com.base.vo.uum.PageHelperUumUser;
import com.base.vo.uum.UumUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping(value = "/uum" ,produces = "application/json;charset=UTF-8")
public class UumController {
    @Autowired
    private UumUserService uumUserService;

    private Logger logger = LoggerFactory.getLogger(UumController.class);

    @PostMapping("/login")
    public String userLogin(UumUser uumUser, HttpServletRequest req, HttpServletResponse resp) {
        return uumUserService.login(uumUser, req, resp);
    }

    @GetMapping("/logout")
    @Async
    public void logout(HttpServletRequest req, HttpServletResponse resp) {
        uumUserService.logout(req,resp);
    }

    @PostMapping("/getUumUser")
    public String getUumUser(PageHelperUumUser pageHelperUumUser) {
        return uumUserService.getUumUser(pageHelperUumUser);
    }

    @PostMapping("/saveUumUser")
    public String saveUumUser(UumUser uumUser) {
        return uumUserService.saveUumUser(uumUser);
    }

    @GetMapping("/getLastWeekViews")
    public String getLastWeekViews(){
        return uumUserService.getLastWeekViews();
    }

    /**
     * 获取图片验证码
     * @param request
     * @param response
     */
    @GetMapping(value = "/getVerifylogin")
    public void getVerifyLogin(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            logger.error("获取验证码失败>>>>   ", e);
        }
    }

    @PostMapping("/uptUserPassword")
    public String uptUserPassword(UumUser uumUser) {
        return uumUserService.uptUserPassword(uumUser);
    }
}
